package com.blogs.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

import com.blogs.component.HomeBaseActivity;
import com.blogs.component.NewsAdapter;
import com.blogs.component.RefreshListView;
import com.blogs.component.RefreshListView.OnMoreListener;
import com.blogs.component.RefreshListView.OnRefreshListener;
import com.blogs.entity.AppEnums;
import com.blogs.entity.NewsFeed;
import com.blogs.service.GetNewLine;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.LoadingGif;
import com.cnblogs.R;

public class NewsActivity extends HomeBaseActivity implements OnItemClickListener,OnItemLongClickListener{
	enum getDataMethod{
		init,//初始化
		more,//点击更多
		pull//点击下拉
	}//获取数据方式
	private DBSharedPreferences sp;
	private ArrayList<NewsFeed> feedList;
	private LoadingGif loadingGif;
	private LinearLayout main_list_ll;
	private RefreshListView listView; 
	private NewsAdapter adapter;
	
	private int curpage=1;
	
	private TextView title_include_title;
	private RelativeLayout title_top_bg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		main_list_ll=(LinearLayout)findViewById(R.id.main_list_ll);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_title.setText("新闻");
		title_top_bg=(RelativeLayout)headview;
		
		listView=new RefreshListView(this);
		listView.setonRefreshListener(new OnRefreshListener() {
			
			public void onRefresh() {
				// TODO Auto-generated method stub
				//刷新
				RefreshData();
			}
		});
		listView.setonMoreListener(new OnMoreListener() {
			
			public void onGetMore() {
				// TODO Auto-generated method stub
				//更多
				GetMoreData();
			}
		});
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
		loadingGif=new LoadingGif(this, listView, main_list_ll,sp);
		loadingGif.setOnReloadListener(new LoadingGif.OnReloadListener() {
			
			public void onClick() {
				// TODO Auto-generated method stub
				loadingGif.showLoading();
				RefreshData();
			}
		});
		InitSkin();
		this.requestData();
		
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			main_list_ll.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			main_list_ll.getBackground().setLevel(1);
		}
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		InitSkin();
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}
	private void requestData()
	{
		loadingGif.showLoading();
		this.sendHomeTimeLine("","",getDataMethod.init);
	}
	private void RefreshData()
	{
		curpage=1;
		String sinceid=feedList!=null?feedList.get(0).new_id:"";
		this.sendHomeTimeLine(sinceid,"",getDataMethod.pull);
	}
	private void GetMoreData()
	{
		curpage++;
		String maxid=feedList.get(feedList.size()-1).new_id;
		this.sendHomeTimeLine("",maxid,getDataMethod.more);
	}
	public void updateRequestData()
	{
		if(adapter!=null)
		{
			adapter.notifyDataSetChanged();
		}
		else
		{
			adapter=new NewsAdapter(this, feedList);
			listView.setAdapter(adapter);
		}
		loadingGif.showData();
		listView.onRefreshComplete();
		listView.onMoreComplete();
	}
	private void sendHomeTimeLine(String since_id,String max_id,final getDataMethod gdm)
	{
		new GetNewLine(this,curpage, since_id,max_id,new GetNewLine.IMsgCallBack() {
			
			public void onSuccess(ArrayList<NewsFeed> feeds) {
				// TODO Auto-generated method stub
				if(feedList==null)
					feedList=new ArrayList<NewsFeed>();
				if(gdm==getDataMethod.init||gdm==getDataMethod.pull)
				{
					feedList.clear();
				}
				for(int i=0;i<feeds.size();i++)
				{
					feedList.add(feeds.get(i));
				}
				updateRequestData();
				listView.onRefreshComplete();
			}
			
			public void onFailed() {
				// TODO Auto-generated method stub
				loadingGif.showFiale();
			}
		}).loadData();
	}
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent=new Intent();
		intent.setClass(this, ContentActivity.class);
		intent.putExtra("Id", feedList.get(position-1).new_id);
		intent.putExtra("Title", feedList.get(position-1).title);
		intent.putExtra("Author", feedList.get(position-1).author);
		intent.putExtra("publicDate", feedList.get(position-1).public_time);
		intent.putExtra("ViewCount", feedList.get(position-1).hit);
		intent.putExtra("CommentCount", feedList.get(position-1).comment);
		intent.putExtra("url", feedList.get(position-1).new_url);
		intent.putExtra("type",AppEnums.EnumContentType.news);
		startActivity(intent);
	}
	public boolean onItemLongClick(AdapterView<?> parent, View view,final int position,
			long id) {
		// TODO Auto-generated method stub
		String[] items = { "查看正文", "查看评论", "取消" };
		new AlertDialog.Builder(NewsActivity.this).setItems(items, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				switch (which) {
				case 0:
					Intent intent_con = new Intent();
					intent_con.setClass(NewsActivity.this, ContentActivity.class);
					intent_con.putExtra("Id", feedList.get(position - 1).new_id);
					intent_con.putExtra("Title", feedList.get(position - 1).title);
					intent_con.putExtra("Author", feedList.get(position - 1).author);
					intent_con.putExtra("publicDate", feedList.get(position - 1).public_time);
					intent_con.putExtra("ViewCount", feedList.get(position - 1).hit);
					intent_con.putExtra("CommentCount", feedList.get(position - 1).comment);
					intent_con.putExtra("url", feedList.get(position - 1).new_url);
					intent_con.putExtra("type", AppEnums.EnumContentType.blog);
					startActivity(intent_con);
					break;
				case 1:
					Intent intent_com=new Intent();
					intent_com.setClass(NewsActivity.this,CommentActivity.class);
					intent_com.putExtra("comment_id",  feedList.get(position - 1).new_id);
					intent_com.putExtra("Title", feedList.get(position - 1).title);
					intent_com.putExtra("type", AppEnums.EnumContentType.news);
					startActivity(intent_com);
					break;
				default:

					break;
				}
			}
		}).create().show();
		return true;
	}
}
